package com.example.module_notice.ui.notice

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.RequestEvent
import com.example.lib_common.base.state.Success
import com.example.module_notice.ui.model.AnnouncementEntityItem
import com.example.module_notice.ui.model.NoticeDetailEntity
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class NoticeViewModel @Inject constructor(private val repo : NoticeRepository) : BaseViewModel() {
    private val mNoticeState_ = MutableStateFlow<RequestEvent>(Loading)
    val mNoticeState = mNoticeState_.asStateFlow()

    val mNoticeList = mutableListOf<AnnouncementEntityItem>()

    private val mNoticeDetailState_ = MutableStateFlow<RequestEvent>(Loading)
    val mNoticeDetailState = mNoticeDetailState_.asStateFlow()
    var mNoticeDetail = MutableLiveData<NoticeDetailEntity>()
    fun getNoticeList(userId : Int){
        execute(
            repo.getAnnouncementByUserId(userId),
            onStart = {
                mNoticeState_.value = Loading
            },
            onError = {
                mNoticeState_.value = Failed("数据获取失败"+it)
            },
            onFinish = {
                mNoticeList.clear()
                mNoticeList.addAll(it.data)
                Log.d("notice-log---",it.data.toString())
                mNoticeState_.value = Success
            }
        )
    }
    fun getNoticeNotice(id : Int){
        execute(
            repo.getAnnouncementsDetail(id),
            onStart = {
                mNoticeDetailState_.value = Loading
            },
            onError = {
                mNoticeDetailState_.value = Failed("数据获取失败"+it)
            },
            onFinish = {
                mNoticeDetailState_.value = Success
                mNoticeDetail.value = it.data
                Log.e("notice---------","mNoticeDetail: ${it.data}+${mNoticeDetail.toString()}")
            }
        )
    }
}